
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Django Utils &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Validators" href="validators.html" />
    <link rel="prev" title="django.urls functions for use in URLconfs" href="urls.html" />



 
<script type="text/javascript" src="../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="urls.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;django.urls&lt;/span&gt;&lt;/code&gt; functions for use in URLconfs">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="validators.html" title="Validators">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-utils">
            
  <div class="section" id="s-module-django.utils">
<span id="s-django-utils"></span><span id="module-django.utils"></span><span id="django-utils"></span><h1>Django Utils<a class="headerlink" href="#module-django.utils" title="Permalink to this headline">¶</a></h1>
<p>This document covers all stable modules in <code class="docutils literal notranslate"><span class="pre">django.utils</span></code>. Most of the
modules in <code class="docutils literal notranslate"><span class="pre">django.utils</span></code> are designed for internal use and only the
following parts can be considered stable and thus backwards compatible as per
the <a class="reference internal" href="../internals/release-process.html#internal-release-deprecation-policy"><span class="std std-ref">internal release deprecation policy</span></a>.</p>
<div class="section" id="s-module-django.utils.cache">
<span id="s-django-utils-cache"></span><span id="module-django.utils.cache"></span><span id="django-utils-cache"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.cache</span></code><a class="headerlink" href="#module-django.utils.cache" title="Permalink to this headline">¶</a></h2>
<p>This module contains helper functions for controlling HTTP caching. It does so
by managing the <code class="docutils literal notranslate"><span class="pre">Vary</span></code> header of responses. It includes functions to patch
the header of response objects directly and decorators that change functions to
do that header-patching themselves.</p>
<p>For information on the <code class="docutils literal notranslate"><span class="pre">Vary</span></code> header, see <span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7231.html#section-7.1.4"><strong>RFC 7231#section-7.1.4</strong></a>.</p>
<p>Essentially, the <code class="docutils literal notranslate"><span class="pre">Vary</span></code> HTTP header defines which headers a cache should take
into account when building its cache key. Requests with the same path but
different header content for headers named in <code class="docutils literal notranslate"><span class="pre">Vary</span></code> need to get different
cache keys to prevent delivery of wrong content.</p>
<p>For example, <a class="reference internal" href="../topics/i18n/index.html"><span class="doc">internationalization</span></a> middleware would
need to distinguish caches by the <code class="docutils literal notranslate"><span class="pre">Accept-language</span></code> header.</p>
<dl class="function">
<dt id="django.utils.cache.patch_cache_control">
<code class="descname">patch_cache_control</code>(<em>response</em>, <em>**kwargs</em>)<a class="reference internal" href="../_modules/django/utils/cache.html#patch_cache_control"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.cache.patch_cache_control" title="Permalink to this definition">¶</a></dt>
<dd><p>This function patches the <code class="docutils literal notranslate"><span class="pre">Cache-Control</span></code> header by adding all keyword
arguments to it. The transformation is as follows:</p>
<ul class="simple">
<li>All keyword parameter names are turned to lowercase, and underscores
are converted to hyphens.</li>
<li>If the value of a parameter is <code class="docutils literal notranslate"><span class="pre">True</span></code> (exactly <code class="docutils literal notranslate"><span class="pre">True</span></code>, not just a
true value), only the parameter name is added to the header.</li>
<li>All other parameters are added with their value, after applying
<code class="docutils literal notranslate"><span class="pre">str()</span></code> to it.</li>
</ul>
</dd></dl>

<dl class="function">
<dt id="django.utils.cache.get_max_age">
<code class="descname">get_max_age</code>(<em>response</em>)<a class="reference internal" href="../_modules/django/utils/cache.html#get_max_age"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.cache.get_max_age" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the max-age from the response Cache-Control header as an integer
(or <code class="docutils literal notranslate"><span class="pre">None</span></code> if it wasn’t found or wasn’t an integer).</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.cache.patch_response_headers">
<code class="descname">patch_response_headers</code>(<em>response</em>, <em>cache_timeout=None</em>)<a class="reference internal" href="../_modules/django/utils/cache.html#patch_response_headers"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.cache.patch_response_headers" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds some useful headers to the given <code class="docutils literal notranslate"><span class="pre">HttpResponse</span></code> object:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">Expires</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">Cache-Control</span></code></li>
</ul>
<p>Each header is only added if it isn’t already set.</p>
<p><code class="docutils literal notranslate"><span class="pre">cache_timeout</span></code> is in seconds. The <a class="reference internal" href="settings.html#std:setting-CACHE_MIDDLEWARE_SECONDS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">CACHE_MIDDLEWARE_SECONDS</span></code></a>
setting is used by default.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.cache.add_never_cache_headers">
<code class="descname">add_never_cache_headers</code>(<em>response</em>)<a class="reference internal" href="../_modules/django/utils/cache.html#add_never_cache_headers"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.cache.add_never_cache_headers" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds a <code class="docutils literal notranslate"><span class="pre">Cache-Control:</span> <span class="pre">max-age=0,</span> <span class="pre">no-cache,</span> <span class="pre">no-store,</span> <span class="pre">must-revalidate</span></code>
header to a response to indicate that a page should never be cached.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.cache.patch_vary_headers">
<code class="descname">patch_vary_headers</code>(<em>response</em>, <em>newheaders</em>)<a class="reference internal" href="../_modules/django/utils/cache.html#patch_vary_headers"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.cache.patch_vary_headers" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds (or updates) the <code class="docutils literal notranslate"><span class="pre">Vary</span></code> header in the given <code class="docutils literal notranslate"><span class="pre">HttpResponse</span></code> object.
<code class="docutils literal notranslate"><span class="pre">newheaders</span></code> is a list of header names that should be in <code class="docutils literal notranslate"><span class="pre">Vary</span></code>.
Existing headers in <code class="docutils literal notranslate"><span class="pre">Vary</span></code> aren’t removed.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.cache.get_cache_key">
<code class="descname">get_cache_key</code>(<em>request</em>, <em>key_prefix=None</em>)<a class="reference internal" href="../_modules/django/utils/cache.html#get_cache_key"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.cache.get_cache_key" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a cache key based on the request path. It can be used in the
request phase because it pulls the list of headers to take into account
from the global path registry and uses those to build a cache key to
check against.</p>
<p>If there is no headerlist stored, the page needs to be rebuilt, so this
function returns <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.cache.learn_cache_key">
<code class="descname">learn_cache_key</code>(<em>request</em>, <em>response</em>, <em>cache_timeout=None</em>, <em>key_prefix=None</em>)<a class="reference internal" href="../_modules/django/utils/cache.html#learn_cache_key"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.cache.learn_cache_key" title="Permalink to this definition">¶</a></dt>
<dd><p>Learns what headers to take into account for some request path from the
response object. It stores those headers in a global path registry so that
later access to that path will know what headers to take into account
without building the response object itself. The headers are named in
the <code class="docutils literal notranslate"><span class="pre">Vary</span></code> header of the response, but we want to prevent response
generation.</p>
<p>The list of headers to use for cache key generation is stored in the same
cache as the pages themselves. If the cache ages some data out of the
cache, this just means that we have to build the response once to get at
the Vary header and so at the list of headers to use for the cache key.</p>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.dateparse">
<span id="s-django-utils-dateparse"></span><span id="module-django.utils.dateparse"></span><span id="django-utils-dateparse"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.dateparse</span></code><a class="headerlink" href="#module-django.utils.dateparse" title="Permalink to this headline">¶</a></h2>
<p>The functions defined in this module share the following properties:</p>
<ul class="simple">
<li>They accept strings in ISO 8601 date/time formats (or some close
alternatives) and return objects from the corresponding classes in Python’s
<a class="reference external" href="https://docs.python.org/3/library/datetime.html#module-datetime" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module.</li>
<li>They raise <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ValueError" title="(in Python v3.8)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if their input is well formatted but isn’t a
valid date or time.</li>
<li>They return <code class="docutils literal notranslate"><span class="pre">None</span></code> if it isn’t well formatted at all.</li>
<li>They accept up to picosecond resolution in input, but they truncate it to
microseconds, since that’s what Python supports.</li>
</ul>
<dl class="function">
<dt id="django.utils.dateparse.parse_date">
<code class="descname">parse_date</code>(<em>value</em>)<a class="reference internal" href="../_modules/django/utils/dateparse.html#parse_date"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.dateparse.parse_date" title="Permalink to this definition">¶</a></dt>
<dd><p>Parses a string and returns a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.date</span></code></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.dateparse.parse_time">
<code class="descname">parse_time</code>(<em>value</em>)<a class="reference internal" href="../_modules/django/utils/dateparse.html#parse_time"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.dateparse.parse_time" title="Permalink to this definition">¶</a></dt>
<dd><p>Parses a string and returns a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.time" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.time</span></code></a>.</p>
<p>UTC offsets aren’t supported; if <code class="docutils literal notranslate"><span class="pre">value</span></code> describes one, the result is
<code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.dateparse.parse_datetime">
<code class="descname">parse_datetime</code>(<em>value</em>)<a class="reference internal" href="../_modules/django/utils/dateparse.html#parse_datetime"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.dateparse.parse_datetime" title="Permalink to this definition">¶</a></dt>
<dd><p>Parses a string and returns a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code></a>.</p>
<p>UTC offsets are supported; if <code class="docutils literal notranslate"><span class="pre">value</span></code> describes one, the result’s
<code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> attribute is a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.timezone" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.timezone</span></code></a> instance.</p>
<div class="versionchanged">
<span class="title">Changed in Django 2.2:</span> <p>In older versions, the <code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> attribute is a
<a class="reference internal" href="#django.utils.timezone.FixedOffset" title="django.utils.timezone.FixedOffset"><code class="xref py py-class docutils literal notranslate"><span class="pre">FixedOffset</span></code></a> instance.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="django.utils.dateparse.parse_duration">
<code class="descname">parse_duration</code>(<em>value</em>)<a class="reference internal" href="../_modules/django/utils/dateparse.html#parse_duration"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.dateparse.parse_duration" title="Permalink to this definition">¶</a></dt>
<dd><p>Parses a string and returns a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.timedelta" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.timedelta</span></code></a>.</p>
<p>Expects data in the format <code class="docutils literal notranslate"><span class="pre">&quot;DD</span> <span class="pre">HH:MM:SS.uuuuuu&quot;</span></code> or as specified by ISO
8601 (e.g. <code class="docutils literal notranslate"><span class="pre">P4DT1H15M20S</span></code> which is equivalent to <code class="docutils literal notranslate"><span class="pre">4</span> <span class="pre">1:15:20</span></code>) or
PostgreSQL’s day-time interval format (e.g. <code class="docutils literal notranslate"><span class="pre">3</span> <span class="pre">days</span> <span class="pre">04:05:06</span></code>).</p>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.decorators">
<span id="s-django-utils-decorators"></span><span id="module-django.utils.decorators"></span><span id="django-utils-decorators"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.decorators</span></code><a class="headerlink" href="#module-django.utils.decorators" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="django.utils.decorators.method_decorator">
<code class="descname">method_decorator</code>(<em>decorator</em>, <em>name=''</em>)<a class="reference internal" href="../_modules/django/utils/decorators.html#method_decorator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.decorators.method_decorator" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts a function decorator into a method decorator. It can be used to
decorate methods or classes; in the latter case, <code class="docutils literal notranslate"><span class="pre">name</span></code> is the name
of the method to be decorated and is required.</p>
<p><code class="docutils literal notranslate"><span class="pre">decorator</span></code> may also be a list or tuple of functions. They are wrapped
in reverse order so that the call order is the order in which the functions
appear in the list/tuple.</p>
<p>See <a class="reference internal" href="../topics/class-based-views/intro.html#id1"><span class="std std-ref">decorating class based views</span></a> for
example usage.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.decorators.decorator_from_middleware">
<code class="descname">decorator_from_middleware</code>(<em>middleware_class</em>)<a class="reference internal" href="../_modules/django/utils/decorators.html#decorator_from_middleware"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.decorators.decorator_from_middleware" title="Permalink to this definition">¶</a></dt>
<dd><p>Given a middleware class, returns a view decorator. This lets you use
middleware functionality on a per-view basis. The middleware is created
with no params passed.</p>
<p>It assumes middleware that’s compatible with the old style of Django 1.9
and earlier (having methods like <code class="docutils literal notranslate"><span class="pre">process_request()</span></code>,
<code class="docutils literal notranslate"><span class="pre">process_exception()</span></code>, and <code class="docutils literal notranslate"><span class="pre">process_response()</span></code>).</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.decorators.decorator_from_middleware_with_args">
<code class="descname">decorator_from_middleware_with_args</code>(<em>middleware_class</em>)<a class="reference internal" href="../_modules/django/utils/decorators.html#decorator_from_middleware_with_args"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.decorators.decorator_from_middleware_with_args" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <code class="docutils literal notranslate"><span class="pre">decorator_from_middleware</span></code>, but returns a function
that accepts the arguments to be passed to the middleware_class.
For example, the <a class="reference internal" href="../topics/cache.html#django.views.decorators.cache.cache_page" title="django.views.decorators.cache.cache_page"><code class="xref py py-func docutils literal notranslate"><span class="pre">cache_page()</span></code></a>
decorator is created from the <code class="docutils literal notranslate"><span class="pre">CacheMiddleware</span></code> like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">cache_page</span> <span class="o">=</span> <span class="n">decorator_from_middleware_with_args</span><span class="p">(</span><span class="n">CacheMiddleware</span><span class="p">)</span>

<span class="nd">@cache_page</span><span class="p">(</span><span class="mi">3600</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">pass</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.encoding">
<span id="s-django-utils-encoding"></span><span id="module-django.utils.encoding"></span><span id="django-utils-encoding"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.encoding</span></code><a class="headerlink" href="#module-django.utils.encoding" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="django.utils.encoding.python_2_unicode_compatible">
<code class="descname">python_2_unicode_compatible</code>()<a class="reference internal" href="../_modules/django/utils/six.html#python_2_unicode_compatible"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.encoding.python_2_unicode_compatible" title="Permalink to this definition">¶</a></dt>
<dd><p>A decorator that defines <code class="docutils literal notranslate"><span class="pre">__unicode__</span></code> and <code class="docutils literal notranslate"><span class="pre">__str__</span></code> methods under
Python 2. Under Python 3 it does nothing.</p>
<p>To support Python 2 and 3 with a single code base, define a <code class="docutils literal notranslate"><span class="pre">__str__</span></code>
method returning text (use <code class="docutils literal notranslate"><span class="pre">six.text_type()</span></code> if you’re doing some
casting) and apply this decorator to the class.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.smart_text">
<code class="descname">smart_text</code>(<em>s</em>, <em>encoding='utf-8'</em>, <em>strings_only=False</em>, <em>errors='strict'</em>)<a class="reference internal" href="../_modules/django/utils/encoding.html#smart_text"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.encoding.smart_text" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a <code class="docutils literal notranslate"><span class="pre">str</span></code> object representing arbitrary object <code class="docutils literal notranslate"><span class="pre">s</span></code>. Treats
bytestrings using the <code class="docutils literal notranslate"><span class="pre">encoding</span></code> codec.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">strings_only</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, don’t convert (some) non-string-like
objects.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.is_protected_type">
<code class="descname">is_protected_type</code>(<em>obj</em>)<a class="reference internal" href="../_modules/django/utils/encoding.html#is_protected_type"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.encoding.is_protected_type" title="Permalink to this definition">¶</a></dt>
<dd><p>Determine if the object instance is of a protected type.</p>
<p>Objects of protected types are preserved as-is when passed to
<code class="docutils literal notranslate"><span class="pre">force_text(strings_only=True)</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.force_text">
<code class="descname">force_text</code>(<em>s</em>, <em>encoding='utf-8'</em>, <em>strings_only=False</em>, <em>errors='strict'</em>)<a class="reference internal" href="../_modules/django/utils/encoding.html#force_text"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.encoding.force_text" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to <code class="docutils literal notranslate"><span class="pre">smart_text</span></code>, except that lazy instances are resolved to
strings, rather than kept as lazy objects.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">strings_only</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, don’t convert (some) non-string-like
objects.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.smart_bytes">
<code class="descname">smart_bytes</code>(<em>s</em>, <em>encoding='utf-8'</em>, <em>strings_only=False</em>, <em>errors='strict'</em>)<a class="reference internal" href="../_modules/django/utils/encoding.html#smart_bytes"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.encoding.smart_bytes" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a bytestring version of arbitrary object <code class="docutils literal notranslate"><span class="pre">s</span></code>, encoded as
specified in <code class="docutils literal notranslate"><span class="pre">encoding</span></code>.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">strings_only</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, don’t convert (some) non-string-like
objects.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.force_bytes">
<code class="descname">force_bytes</code>(<em>s</em>, <em>encoding='utf-8'</em>, <em>strings_only=False</em>, <em>errors='strict'</em>)<a class="reference internal" href="../_modules/django/utils/encoding.html#force_bytes"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.encoding.force_bytes" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to <code class="docutils literal notranslate"><span class="pre">smart_bytes</span></code>, except that lazy instances are resolved to
bytestrings, rather than kept as lazy objects.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">strings_only</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, don’t convert (some) non-string-like
objects.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.smart_str">
<code class="descname">smart_str</code>(<em>s</em>, <em>encoding='utf-8'</em>, <em>strings_only=False</em>, <em>errors='strict'</em>)<a class="headerlink" href="#django.utils.encoding.smart_str" title="Permalink to this definition">¶</a></dt>
<dd><p>Alias of <a class="reference internal" href="#django.utils.encoding.smart_text" title="django.utils.encoding.smart_text"><code class="xref py py-func docutils literal notranslate"><span class="pre">smart_text()</span></code></a>. This function returns a <code class="docutils literal notranslate"><span class="pre">str</span></code> or a lazy
string.</p>
<p>For instance, this is suitable for writing to <a class="reference external" href="https://docs.python.org/3/library/sys.html#sys.stdout" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a>.</p>
<p>Alias of <a class="reference internal" href="#django.utils.encoding.smart_bytes" title="django.utils.encoding.smart_bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">smart_bytes()</span></code></a> on Python 2 (in older versions of Django that
support it).</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.force_str">
<code class="descname">force_str</code>(<em>s</em>, <em>encoding='utf-8'</em>, <em>strings_only=False</em>, <em>errors='strict'</em>)<a class="headerlink" href="#django.utils.encoding.force_str" title="Permalink to this definition">¶</a></dt>
<dd><p>Alias of <a class="reference internal" href="#django.utils.encoding.force_text" title="django.utils.encoding.force_text"><code class="xref py py-func docutils literal notranslate"><span class="pre">force_text()</span></code></a>. This function always returns a <code class="docutils literal notranslate"><span class="pre">str</span></code>.</p>
<p>Alias of <a class="reference internal" href="#django.utils.encoding.force_bytes" title="django.utils.encoding.force_bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">force_bytes()</span></code></a> on Python 2 (in older versions of Django that
support it).</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.iri_to_uri">
<code class="descname">iri_to_uri</code>(<em>iri</em>)<a class="reference internal" href="../_modules/django/utils/encoding.html#iri_to_uri"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.encoding.iri_to_uri" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert an Internationalized Resource Identifier (IRI) portion to a URI
portion that is suitable for inclusion in a URL.</p>
<p>This is the algorithm from section 3.1 of <span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3987.html#section-3.1"><strong>RFC 3987#section-3.1</strong></a>, slightly
simplified since the input is assumed to be a string rather than an
arbitrary byte stream.</p>
<p>Takes an IRI (string or UTF-8 bytes) and returns a string containing the
encoded result.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.uri_to_iri">
<code class="descname">uri_to_iri</code>(<em>uri</em>)<a class="reference internal" href="../_modules/django/utils/encoding.html#uri_to_iri"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.encoding.uri_to_iri" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts a Uniform Resource Identifier into an Internationalized Resource
Identifier.</p>
<p>This is an algorithm from section 3.2 of <span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3987.html#section-3.2"><strong>RFC 3987#section-3.2</strong></a>.</p>
<p>Takes a URI in ASCII bytes and returns a string containing the encoded
result.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.filepath_to_uri">
<code class="descname">filepath_to_uri</code>(<em>path</em>)<a class="reference internal" href="../_modules/django/utils/encoding.html#filepath_to_uri"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.encoding.filepath_to_uri" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a file system path to a URI portion that is suitable for inclusion
in a URL. The path is assumed to be either UTF-8 bytes or string.</p>
<p>This method will encode certain characters that would normally be
recognized as special characters for URIs.  Note that this method does not
encode the ‘ character, as it is a valid character within URIs. See
<code class="docutils literal notranslate"><span class="pre">encodeURIComponent()</span></code> JavaScript function for more details.</p>
<p>Returns an ASCII string containing the encoded result.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.encoding.escape_uri_path">
<code class="descname">escape_uri_path</code>(<em>path</em>)<a class="reference internal" href="../_modules/django/utils/encoding.html#escape_uri_path"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.encoding.escape_uri_path" title="Permalink to this definition">¶</a></dt>
<dd><p>Escapes the unsafe characters from the path portion of a Uniform Resource
Identifier (URI).</p>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.feedgenerator">
<span id="s-django-utils-feedgenerator"></span><span id="module-django.utils.feedgenerator"></span><span id="django-utils-feedgenerator"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.feedgenerator</span></code><a class="headerlink" href="#module-django.utils.feedgenerator" title="Permalink to this headline">¶</a></h2>
<p>Sample usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">feedgenerator</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">feed</span> <span class="o">=</span> <span class="n">feedgenerator</span><span class="o">.</span><span class="n">Rss201rev2Feed</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">title</span><span class="o">=</span><span class="s2">&quot;Poynter E-Media Tidbits&quot;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">link</span><span class="o">=</span><span class="s2">&quot;http://www.poynter.org/column.asp?id=31&quot;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">description</span><span class="o">=</span><span class="s2">&quot;A group Weblog by the sharpest minds in online media/journalism/publishing.&quot;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">language</span><span class="o">=</span><span class="s2">&quot;en&quot;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">feed</span><span class="o">.</span><span class="n">add_item</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">title</span><span class="o">=</span><span class="s2">&quot;Hello&quot;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">link</span><span class="o">=</span><span class="s2">&quot;http://www.holovaty.com/test/&quot;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">description</span><span class="o">=</span><span class="s2">&quot;Testing.&quot;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;test.rss&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">feed</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>For simplifying the selection of a generator use <code class="docutils literal notranslate"><span class="pre">feedgenerator.DefaultFeed</span></code>
which is currently <code class="docutils literal notranslate"><span class="pre">Rss201rev2Feed</span></code></p>
<p>For definitions of the different versions of RSS, see:
<a class="reference external" href="https://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/02/04/incompatible-rss">https://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/02/04/incompatible-rss</a></p>
<dl class="function">
<dt id="django.utils.feedgenerator.get_tag_uri">
<code class="descname">get_tag_uri</code>(<em>url</em>, <em>date</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#get_tag_uri"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.get_tag_uri" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a TagURI.</p>
<p>See <a class="reference external" href="https://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id">https://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id</a></p>
</dd></dl>

<div class="section" id="s-syndicationfeed">
<span id="syndicationfeed"></span><h3><code class="docutils literal notranslate"><span class="pre">SyndicationFeed</span></code><a class="headerlink" href="#syndicationfeed" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.utils.feedgenerator.SyndicationFeed">
<em class="property">class </em><code class="descname">SyndicationFeed</code><a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed" title="Permalink to this definition">¶</a></dt>
<dd><p>Base class for all syndication feeds. Subclasses should provide write().</p>
<dl class="method">
<dt id="django.utils.feedgenerator.SyndicationFeed.__init__">
<code class="descname">__init__</code>(<em>title</em>, <em>link</em>, <em>description</em>, <em>language=None</em>, <em>author_email=None</em>, <em>author_name=None</em>, <em>author_link=None</em>, <em>subtitle=None</em>, <em>categories=None</em>, <em>feed_url=None</em>, <em>feed_copyright=None</em>, <em>feed_guid=None</em>, <em>ttl=None</em>, <em>**kwargs</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed.__init__"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Initialize the feed with the given dictionary of metadata, which applies
to the entire feed.</p>
<p>Any extra keyword arguments you pass to <code class="docutils literal notranslate"><span class="pre">__init__</span></code> will be stored in
<code class="docutils literal notranslate"><span class="pre">self.feed</span></code>.</p>
<p>All parameters should be strings, except <code class="docutils literal notranslate"><span class="pre">categories</span></code>, which should
be a sequence of strings.</p>
</dd></dl>

<dl class="method">
<dt id="django.utils.feedgenerator.SyndicationFeed.add_item">
<code class="descname">add_item</code>(<em>title</em>, <em>link</em>, <em>description</em>, <em>author_email=None</em>, <em>author_name=None</em>, <em>author_link=None</em>, <em>pubdate=None</em>, <em>comments=None</em>, <em>unique_id=None</em>, <em>categories=()</em>, <em>item_copyright=None</em>, <em>ttl=None</em>, <em>updateddate=None</em>, <em>enclosures=None</em>, <em>**kwargs</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed.add_item"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed.add_item" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds an item to the feed. All args are expected to be strings except
<code class="docutils literal notranslate"><span class="pre">pubdate</span></code> and <code class="docutils literal notranslate"><span class="pre">updateddate</span></code>, which are <code class="docutils literal notranslate"><span class="pre">datetime.datetime</span></code>
objects, and <code class="docutils literal notranslate"><span class="pre">enclosures</span></code>, which is a list of <code class="docutils literal notranslate"><span class="pre">Enclosure</span></code> instances.</p>
</dd></dl>

<dl class="method">
<dt id="django.utils.feedgenerator.SyndicationFeed.num_items">
<code class="descname">num_items</code>()<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed.num_items"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed.num_items" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="django.utils.feedgenerator.SyndicationFeed.root_attributes">
<code class="descname">root_attributes</code>()<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed.root_attributes"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed.root_attributes" title="Permalink to this definition">¶</a></dt>
<dd><p>Return extra attributes to place on the root (i.e. feed/channel)
element. Called from <code class="docutils literal notranslate"><span class="pre">write()</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="django.utils.feedgenerator.SyndicationFeed.add_root_elements">
<code class="descname">add_root_elements</code>(<em>handler</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed.add_root_elements"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed.add_root_elements" title="Permalink to this definition">¶</a></dt>
<dd><p>Add elements in the root (i.e. feed/channel) element.
Called from <code class="docutils literal notranslate"><span class="pre">write()</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="django.utils.feedgenerator.SyndicationFeed.item_attributes">
<code class="descname">item_attributes</code>(<em>item</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed.item_attributes"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed.item_attributes" title="Permalink to this definition">¶</a></dt>
<dd><p>Return extra attributes to place on each item (i.e. item/entry)
element.</p>
</dd></dl>

<dl class="method">
<dt id="django.utils.feedgenerator.SyndicationFeed.add_item_elements">
<code class="descname">add_item_elements</code>(<em>handler</em>, <em>item</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed.add_item_elements"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed.add_item_elements" title="Permalink to this definition">¶</a></dt>
<dd><p>Add elements on each item (i.e. item/entry) element.</p>
</dd></dl>

<dl class="method">
<dt id="django.utils.feedgenerator.SyndicationFeed.write">
<code class="descname">write</code>(<em>outfile</em>, <em>encoding</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed.write"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed.write" title="Permalink to this definition">¶</a></dt>
<dd><p>Outputs the feed in the given encoding to <code class="docutils literal notranslate"><span class="pre">outfile</span></code>, which is a
file-like object. Subclasses should override this.</p>
</dd></dl>

<dl class="method">
<dt id="django.utils.feedgenerator.SyndicationFeed.writeString">
<code class="descname">writeString</code>(<em>encoding</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed.writeString"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed.writeString" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the feed in the given encoding as a string.</p>
</dd></dl>

<dl class="method">
<dt id="django.utils.feedgenerator.SyndicationFeed.latest_post_date">
<code class="descname">latest_post_date</code>()<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#SyndicationFeed.latest_post_date"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.SyndicationFeed.latest_post_date" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the latest <code class="docutils literal notranslate"><span class="pre">pubdate</span></code> or <code class="docutils literal notranslate"><span class="pre">updateddate</span></code> for all items in the
feed. If no items have either of these attributes this returns the
current UTC date/time.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-enclosure">
<span id="enclosure"></span><h3><code class="docutils literal notranslate"><span class="pre">Enclosure</span></code><a class="headerlink" href="#enclosure" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.utils.feedgenerator.Enclosure">
<em class="property">class </em><code class="descname">Enclosure</code><a class="reference internal" href="../_modules/django/utils/feedgenerator.html#Enclosure"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.Enclosure" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents an RSS enclosure</p>
</dd></dl>

</div>
<div class="section" id="s-rssfeed">
<span id="rssfeed"></span><h3><code class="docutils literal notranslate"><span class="pre">RssFeed</span></code><a class="headerlink" href="#rssfeed" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.utils.feedgenerator.RssFeed">
<em class="property">class </em><code class="descname">RssFeed</code>(<em>SyndicationFeed</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#RssFeed"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.RssFeed" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

</div>
<div class="section" id="s-rss201rev2feed">
<span id="rss201rev2feed"></span><h3><code class="docutils literal notranslate"><span class="pre">Rss201rev2Feed</span></code><a class="headerlink" href="#rss201rev2feed" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.utils.feedgenerator.Rss201rev2Feed">
<em class="property">class </em><code class="descname">Rss201rev2Feed</code>(<em>RssFeed</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#Rss201rev2Feed"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.Rss201rev2Feed" title="Permalink to this definition">¶</a></dt>
<dd><p>Spec: <a class="reference external" href="https://cyber.harvard.edu/rss/rss.html">https://cyber.harvard.edu/rss/rss.html</a></p>
</dd></dl>

</div>
<div class="section" id="s-rssuserland091feed">
<span id="rssuserland091feed"></span><h3><code class="docutils literal notranslate"><span class="pre">RssUserland091Feed</span></code><a class="headerlink" href="#rssuserland091feed" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.utils.feedgenerator.RssUserland091Feed">
<em class="property">class </em><code class="descname">RssUserland091Feed</code>(<em>RssFeed</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#RssUserland091Feed"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.RssUserland091Feed" title="Permalink to this definition">¶</a></dt>
<dd><p>Spec: <a class="reference external" href="http://backend.userland.com/rss091">http://backend.userland.com/rss091</a></p>
</dd></dl>

</div>
<div class="section" id="s-atom1feed">
<span id="atom1feed"></span><h3><code class="docutils literal notranslate"><span class="pre">Atom1Feed</span></code><a class="headerlink" href="#atom1feed" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.utils.feedgenerator.Atom1Feed">
<em class="property">class </em><code class="descname">Atom1Feed</code>(<em>SyndicationFeed</em>)<a class="reference internal" href="../_modules/django/utils/feedgenerator.html#Atom1Feed"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.feedgenerator.Atom1Feed" title="Permalink to this definition">¶</a></dt>
<dd><p>Spec: <a class="reference external" href="https://tools.ietf.org/html/rfc4287">https://tools.ietf.org/html/rfc4287</a></p>
</dd></dl>

</div>
</div>
<div class="section" id="s-module-django.utils.functional">
<span id="s-django-utils-functional"></span><span id="module-django.utils.functional"></span><span id="django-utils-functional"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.functional</span></code><a class="headerlink" href="#module-django.utils.functional" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.utils.functional.cached_property">
<em class="property">class </em><code class="descname">cached_property</code>(<em>func</em>, <em>name=None</em>)<a class="reference internal" href="../_modules/django/utils/functional.html#cached_property"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.functional.cached_property" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">&#64;cached_property</span></code> decorator caches the result of a method with a
single <code class="docutils literal notranslate"><span class="pre">self</span></code> argument as a property. The cached result will persist
as long as the instance does, so if the instance is passed around and the
function subsequently invoked, the cached result will be returned.</p>
<p>Consider a typical case, where a view might need to call a model’s method
to perform some computation, before placing the model instance into the
context, where the template might invoke the method once more:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># the model</span>
<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">friends</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># expensive computation</span>
        <span class="o">...</span>
        <span class="k">return</span> <span class="n">friends</span>

<span class="c1"># in the view:</span>
<span class="k">if</span> <span class="n">person</span><span class="o">.</span><span class="n">friends</span><span class="p">():</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>And in the template you would have:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">friend</span> <span class="k">in</span> <span class="nv">person.friends</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Here, <code class="docutils literal notranslate"><span class="pre">friends()</span></code> will be called twice. Since the instance <code class="docutils literal notranslate"><span class="pre">person</span></code> in
the view and the template are the same, decorating the <code class="docutils literal notranslate"><span class="pre">friends()</span></code> method
with <code class="docutils literal notranslate"><span class="pre">&#64;cached_property</span></code> can avoid that:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">cached_property</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">friends</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="o">...</span>
</pre></div>
</div>
<p>Note that as the method is now a property, in Python code it will need to
be accessed appropriately:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># in the view:</span>
<span class="k">if</span> <span class="n">person</span><span class="o">.</span><span class="n">friends</span><span class="p">:</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>The cached value can be treated like an ordinary attribute of the instance:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># clear it, requiring re-computation next time it&#39;s called</span>
<span class="k">del</span> <span class="n">person</span><span class="o">.</span><span class="n">friends</span> <span class="c1"># or delattr(person, &quot;friends&quot;)</span>

<span class="c1"># set a value manually, that will persist on the instance until cleared</span>
<span class="n">person</span><span class="o">.</span><span class="n">friends</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;Huckleberry Finn&quot;</span><span class="p">,</span> <span class="s2">&quot;Tom Sawyer&quot;</span><span class="p">]</span>
</pre></div>
</div>
<p>Because of the way the <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#descriptor-invocation" title="(in Python v3.8)"><span class="xref std std-ref">descriptor protocol</span></a> works, using <code class="docutils literal notranslate"><span class="pre">del</span></code> (or <code class="docutils literal notranslate"><span class="pre">delattr</span></code>) on a
<code class="docutils literal notranslate"><span class="pre">cached_property</span></code> that hasn’t been accessed raises <code class="docutils literal notranslate"><span class="pre">AttributeError</span></code>.</p>
<p>As well as offering potential performance advantages, <code class="docutils literal notranslate"><span class="pre">&#64;cached_property</span></code>
can ensure that an attribute’s value does not change unexpectedly over the
life of an instance. This could occur with a method whose computation is
based on <code class="docutils literal notranslate"><span class="pre">datetime.now()</span></code>, or simply if a change were saved to the
database by some other process in the brief interval between subsequent
invocations of a method on the same instance.</p>
<p>You can make cached properties of methods. For example, if you had an
expensive <code class="docutils literal notranslate"><span class="pre">get_friends()</span></code> method and wanted to allow calling it without
retrieving the cached value, you could write:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">friends</span> <span class="o">=</span> <span class="n">cached_property</span><span class="p">(</span><span class="n">get_friends</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;friends&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>You only need the <code class="docutils literal notranslate"><span class="pre">name</span></code> argument for Python &lt; 3.6 support.</p>
<div class="versionchanged">
<span class="title">Changed in Django 2.2:</span> <p>Older versions of Django require the <code class="docutils literal notranslate"><span class="pre">name</span></code> argument for all versions
of Python.</p>
</div>
<p>While <code class="docutils literal notranslate"><span class="pre">person.get_friends()</span></code> will recompute the friends on each call, the
value of the cached property will persist until you delete it as described
above:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">person</span><span class="o">.</span><span class="n">friends</span>         <span class="c1"># calls first time</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">person</span><span class="o">.</span><span class="n">get_friends</span><span class="p">()</span>   <span class="c1"># calls again</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">person</span><span class="o">.</span><span class="n">friends</span>         <span class="c1"># does not call</span>
<span class="n">x</span> <span class="ow">is</span> <span class="n">z</span>                     <span class="c1"># is True</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p id="cached-property-mangled-name">On Python &lt; 3.6, <code class="docutils literal notranslate"><span class="pre">cached_property</span></code> doesn’t work properly with a
<a class="reference external" href="https://docs.python.org/faq/programming.html#i-try-to-use-spam-and-i-get-an-error-about-someclassname-spam">mangled</a> name unless it’s passed a <code class="docutils literal notranslate"><span class="pre">name</span></code> of the form
<code class="docutils literal notranslate"><span class="pre">_Class__attribute</span></code>:</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">__friends</span> <span class="o">=</span> <span class="n">cached_property</span><span class="p">(</span><span class="n">get_friends</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;_Person__friends&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</dd></dl>

<dl class="function">
<dt id="django.utils.functional.keep_lazy">
<code class="descname">keep_lazy</code>(<em>func</em>, <em>*resultclasses</em>)<a class="reference internal" href="../_modules/django/utils/functional.html#keep_lazy"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.functional.keep_lazy" title="Permalink to this definition">¶</a></dt>
<dd><p>Django offers many utility functions (particularly in <code class="docutils literal notranslate"><span class="pre">django.utils</span></code>)
that take a string as their first argument and do something to that string.
These functions are used by template filters as well as directly in other
code.</p>
<p>If you write your own similar functions and deal with translations, you’ll
face the problem of what to do when the first argument is a lazy
translation object. You don’t want to convert it to a string immediately,
because you might be using this function outside of a view (and hence the
current thread’s locale setting will not be correct).</p>
<p>For cases like this, use the <code class="docutils literal notranslate"><span class="pre">django.utils.functional.keep_lazy()</span></code>
decorator. It modifies the function so that <em>if</em> it’s called with a lazy
translation as one of its arguments, the function evaluation is delayed
until it needs to be converted to a string.</p>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">keep_lazy</span><span class="p">,</span> <span class="n">keep_lazy_text</span>

<span class="k">def</span> <span class="nf">fancy_utility_function</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="o">...</span><span class="p">):</span>
    <span class="c1"># Do some conversion on string &#39;s&#39;</span>
    <span class="o">...</span>
<span class="n">fancy_utility_function</span> <span class="o">=</span> <span class="n">keep_lazy</span><span class="p">(</span><span class="nb">str</span><span class="p">)(</span><span class="n">fancy_utility_function</span><span class="p">)</span>

<span class="c1"># Or more succinctly:</span>
<span class="nd">@keep_lazy</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">fancy_utility_function</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="o">...</span><span class="p">):</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">keep_lazy()</span></code> decorator takes a number of extra arguments (<code class="docutils literal notranslate"><span class="pre">*args</span></code>)
specifying the type(s) that the original function can return. A common
use case is to have functions that return text. For these, you can just
pass the <code class="docutils literal notranslate"><span class="pre">str</span></code> type to <code class="docutils literal notranslate"><span class="pre">keep_lazy</span></code> (or even simpler, use the
<a class="reference internal" href="#django.utils.functional.keep_lazy_text" title="django.utils.functional.keep_lazy_text"><code class="xref py py-func docutils literal notranslate"><span class="pre">keep_lazy_text()</span></code></a> decorator described in the next section).</p>
<p>Using this decorator means you can write your function and assume that the
input is a proper string, then add support for lazy translation objects at
the end.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.functional.keep_lazy_text">
<code class="descname">keep_lazy_text</code>(<em>func</em>)<a class="reference internal" href="../_modules/django/utils/functional.html#keep_lazy_text"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.functional.keep_lazy_text" title="Permalink to this definition">¶</a></dt>
<dd><p>A shortcut for <code class="docutils literal notranslate"><span class="pre">keep_lazy(str)(func)</span></code>.</p>
<p>If you have a function that returns text and you want to be able to take
lazy arguments while delaying their evaluation, simply use this decorator:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">keep_lazy</span><span class="p">,</span> <span class="n">keep_lazy_text</span>

<span class="c1"># Our previous example was:</span>
<span class="nd">@keep_lazy</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">fancy_utility_function</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="o">...</span><span class="p">):</span>
    <span class="o">...</span>

<span class="c1"># Which can be rewritten as:</span>
<span class="nd">@keep_lazy_text</span>
<span class="k">def</span> <span class="nf">fancy_utility_function</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="o">...</span><span class="p">):</span>
    <span class="o">...</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.html">
<span id="s-django-utils-html"></span><span id="module-django.utils.html"></span><span id="django-utils-html"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.html</span></code><a class="headerlink" href="#module-django.utils.html" title="Permalink to this headline">¶</a></h2>
<p>Usually you should build up HTML using Django’s templates to make use of its
autoescape mechanism, using the utilities in <a class="reference internal" href="#module-django.utils.safestring" title="django.utils.safestring: Functions and classes for working with strings that can be displayed safely without further escaping in HTML."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.utils.safestring</span></code></a>
where appropriate. This module provides some additional low level utilities for
escaping HTML.</p>
<dl class="function">
<dt id="django.utils.html.escape">
<code class="descname">escape</code>(<em>text</em>)<a class="reference internal" href="../_modules/django/utils/html.html#escape"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.html.escape" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the given text with ampersands, quotes and angle brackets encoded
for use in HTML. The input is first coerced to a string and the output has
<a class="reference internal" href="#django.utils.safestring.mark_safe" title="django.utils.safestring.mark_safe"><code class="xref py py-func docutils literal notranslate"><span class="pre">mark_safe()</span></code></a> applied.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.html.conditional_escape">
<code class="descname">conditional_escape</code>(<em>text</em>)<a class="reference internal" href="../_modules/django/utils/html.html#conditional_escape"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.html.conditional_escape" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to <code class="docutils literal notranslate"><span class="pre">escape()</span></code>, except that it doesn’t operate on pre-escaped
strings, so it will not double escape.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.html.format_html">
<code class="descname">format_html</code>(<em>format_string</em>, <em>*args</em>, <em>**kwargs</em>)<a class="reference internal" href="../_modules/django/utils/html.html#format_html"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.html.format_html" title="Permalink to this definition">¶</a></dt>
<dd><p>This is similar to <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str.format" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a>, except that it is appropriate for
building up HTML fragments. All args and kwargs are passed through
<a class="reference internal" href="#django.utils.html.conditional_escape" title="django.utils.html.conditional_escape"><code class="xref py py-func docutils literal notranslate"><span class="pre">conditional_escape()</span></code></a> before being passed to <code class="docutils literal notranslate"><span class="pre">str.format()</span></code>.</p>
<p>For the case of building up small HTML fragments, this function is to be
preferred over string interpolation using <code class="docutils literal notranslate"><span class="pre">%</span></code> or <code class="docutils literal notranslate"><span class="pre">str.format()</span></code>
directly, because it applies escaping to all arguments - just like the
template system applies escaping by default.</p>
<p>So, instead of writing:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">mark_safe</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> &lt;b&gt;</span><span class="si">%s</span><span class="s2">&lt;/b&gt; </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
    <span class="n">some_html</span><span class="p">,</span>
    <span class="n">escape</span><span class="p">(</span><span class="n">some_text</span><span class="p">),</span>
    <span class="n">escape</span><span class="p">(</span><span class="n">some_other_text</span><span class="p">),</span>
<span class="p">))</span>
</pre></div>
</div>
<p>You should instead use:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">format_html</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> &lt;b&gt;</span><span class="si">{}</span><span class="s2">&lt;/b&gt; </span><span class="si">{}</span><span class="s2">&quot;</span><span class="p">,</span>
    <span class="n">mark_safe</span><span class="p">(</span><span class="n">some_html</span><span class="p">),</span>
    <span class="n">some_text</span><span class="p">,</span>
    <span class="n">some_other_text</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</div>
<p>This has the advantage that you don’t need to apply <a class="reference internal" href="#django.utils.html.escape" title="django.utils.html.escape"><code class="xref py py-func docutils literal notranslate"><span class="pre">escape()</span></code></a> to each
argument and risk a bug and an XSS vulnerability if you forget one.</p>
<p>Note that although this function uses <code class="docutils literal notranslate"><span class="pre">str.format()</span></code> to do the
interpolation, some of the formatting options provided by <code class="docutils literal notranslate"><span class="pre">str.format()</span></code>
(e.g. number formatting) will not work, since all arguments are passed
through <a class="reference internal" href="#django.utils.html.conditional_escape" title="django.utils.html.conditional_escape"><code class="xref py py-func docutils literal notranslate"><span class="pre">conditional_escape()</span></code></a> which (ultimately) calls
<a class="reference internal" href="#django.utils.encoding.force_text" title="django.utils.encoding.force_text"><code class="xref py py-func docutils literal notranslate"><span class="pre">force_text()</span></code></a> on the values.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.html.format_html_join">
<code class="descname">format_html_join</code>(<em>sep</em>, <em>format_string</em>, <em>args_generator</em>)<a class="reference internal" href="../_modules/django/utils/html.html#format_html_join"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.html.format_html_join" title="Permalink to this definition">¶</a></dt>
<dd><p>A wrapper of <a class="reference internal" href="#django.utils.html.format_html" title="django.utils.html.format_html"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_html()</span></code></a>, for the common case of a group of
arguments that need to be formatted using the same format string, and then
joined using <code class="docutils literal notranslate"><span class="pre">sep</span></code>. <code class="docutils literal notranslate"><span class="pre">sep</span></code> is also passed through
<a class="reference internal" href="#django.utils.html.conditional_escape" title="django.utils.html.conditional_escape"><code class="xref py py-func docutils literal notranslate"><span class="pre">conditional_escape()</span></code></a>.</p>
<p><code class="docutils literal notranslate"><span class="pre">args_generator</span></code> should be an iterator that returns the sequence of
<code class="docutils literal notranslate"><span class="pre">args</span></code> that will be passed to <a class="reference internal" href="#django.utils.html.format_html" title="django.utils.html.format_html"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_html()</span></code></a>. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">format_html_join</span><span class="p">(</span>
    <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s2">&quot;&lt;li&gt;</span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="s2">&lt;/li&gt;&quot;</span><span class="p">,</span>
    <span class="p">((</span><span class="n">u</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span> <span class="n">u</span><span class="o">.</span><span class="n">last_name</span><span class="p">)</span> <span class="k">for</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">users</span><span class="p">)</span>
<span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="django.utils.html.strip_tags">
<code class="descname">strip_tags</code>(<em>value</em>)<a class="reference internal" href="../_modules/django/utils/html.html#strip_tags"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.html.strip_tags" title="Permalink to this definition">¶</a></dt>
<dd><p>Tries to remove anything that looks like an HTML tag from the string, that
is anything contained within <code class="docutils literal notranslate"><span class="pre">&lt;&gt;</span></code>.</p>
<p>Absolutely NO guarantee is provided about the resulting string being
HTML safe. So NEVER mark safe the result of a <code class="docutils literal notranslate"><span class="pre">strip_tag</span></code> call without
escaping it first, for example with <a class="reference internal" href="#django.utils.html.escape" title="django.utils.html.escape"><code class="xref py py-func docutils literal notranslate"><span class="pre">escape()</span></code></a>.</p>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">strip_tags</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">value</span></code> is <code class="docutils literal notranslate"><span class="pre">&quot;&lt;b&gt;Joel&lt;/b&gt;</span> <span class="pre">&lt;button&gt;is&lt;/button&gt;</span> <span class="pre">a</span> <span class="pre">&lt;span&gt;slug&lt;/span&gt;&quot;</span></code>
the return value will be <code class="docutils literal notranslate"><span class="pre">&quot;Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug&quot;</span></code>.</p>
<p>If you are looking for a more robust solution, take a look at the <a class="reference external" href="https://pypi.org/project/bleach/">bleach</a> Python library.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.html.html_safe">
<code class="descname">html_safe</code>()<a class="reference internal" href="../_modules/django/utils/html.html#html_safe"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.html.html_safe" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">__html__()</span></code> method on a class helps non-Django templates detect
classes whose output doesn’t require HTML escaping.</p>
<p>This decorator defines the <code class="docutils literal notranslate"><span class="pre">__html__()</span></code> method on the decorated class
by wrapping <code class="docutils literal notranslate"><span class="pre">__str__()</span></code> in <a class="reference internal" href="#django.utils.safestring.mark_safe" title="django.utils.safestring.mark_safe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">mark_safe()</span></code></a>.
Ensure the <code class="docutils literal notranslate"><span class="pre">__str__()</span></code> method does indeed return text that doesn’t
require HTML escaping.</p>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.http">
<span id="s-django-utils-http"></span><span id="module-django.utils.http"></span><span id="django-utils-http"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.http</span></code><a class="headerlink" href="#module-django.utils.http" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="django.utils.http.urlencode">
<code class="descname">urlencode</code>(<em>query</em>, <em>doseq=False</em>)<a class="reference internal" href="../_modules/django/utils/http.html#urlencode"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.http.urlencode" title="Permalink to this definition">¶</a></dt>
<dd><p>A version of Python’s <a class="reference external" href="https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">urllib.parse.urlencode()</span></code></a> function that can
operate on <code class="docutils literal notranslate"><span class="pre">MultiValueDict</span></code> and non-string values.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.http.cookie_date">
<code class="descname">cookie_date</code>(<em>epoch_seconds=None</em>)<a class="reference internal" href="../_modules/django/utils/http.html#cookie_date"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.http.cookie_date" title="Permalink to this definition">¶</a></dt>
<dd><div class="deprecated">
<p><span class="versionmodified">Deprecated since version 2.1: </span>Use <a class="reference internal" href="#django.utils.http.http_date" title="django.utils.http.http_date"><code class="xref py py-func docutils literal notranslate"><span class="pre">http_date()</span></code></a> instead, which follows the latest RFC.</p>
</div>
<p>Formats the time to ensure compatibility with Netscape’s cookie standard.</p>
<p>Accepts a floating point number expressed in seconds since the epoch in
UTC–such as that outputted by <code class="docutils literal notranslate"><span class="pre">time.time()</span></code>. If set to <code class="docutils literal notranslate"><span class="pre">None</span></code>,
defaults to the current time.</p>
<p>Outputs a string in the format <code class="docutils literal notranslate"><span class="pre">Wdy,</span> <span class="pre">DD-Mon-YYYY</span> <span class="pre">HH:MM:SS</span> <span class="pre">GMT</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.http.http_date">
<code class="descname">http_date</code>(<em>epoch_seconds=None</em>)<a class="reference internal" href="../_modules/django/utils/http.html#http_date"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.http.http_date" title="Permalink to this definition">¶</a></dt>
<dd><p>Formats the time to match the <span class="target" id="index-3"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1123.html#section-5.2.14"><strong>RFC 1123#section-5.2.14</strong></a> date format as
specified by HTTP <span class="target" id="index-4"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7231.html#section-7.1.1.1"><strong>RFC 7231#section-7.1.1.1</strong></a>.</p>
<p>Accepts a floating point number expressed in seconds since the epoch in
UTC–such as that outputted by <code class="docutils literal notranslate"><span class="pre">time.time()</span></code>. If set to <code class="docutils literal notranslate"><span class="pre">None</span></code>,
defaults to the current time.</p>
<p>Outputs a string in the format <code class="docutils literal notranslate"><span class="pre">Wdy,</span> <span class="pre">DD</span> <span class="pre">Mon</span> <span class="pre">YYYY</span> <span class="pre">HH:MM:SS</span> <span class="pre">GMT</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.http.base36_to_int">
<code class="descname">base36_to_int</code>(<em>s</em>)<a class="reference internal" href="../_modules/django/utils/http.html#base36_to_int"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.http.base36_to_int" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts a base 36 string to an integer.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.http.int_to_base36">
<code class="descname">int_to_base36</code>(<em>i</em>)<a class="reference internal" href="../_modules/django/utils/http.html#int_to_base36"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.http.int_to_base36" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts a positive integer to a base 36 string.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.http.urlsafe_base64_encode">
<code class="descname">urlsafe_base64_encode</code>(<em>s</em>)<a class="reference internal" href="../_modules/django/utils/http.html#urlsafe_base64_encode"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.http.urlsafe_base64_encode" title="Permalink to this definition">¶</a></dt>
<dd><p>Encodes a bytestring to a base64 string for use in URLs, stripping any
trailing equal signs.</p>
<div class="versionchanged">
<span class="title">Changed in Django 2.2:</span> <p>In older versions, it returns a bytestring instead of a string.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="django.utils.http.urlsafe_base64_decode">
<code class="descname">urlsafe_base64_decode</code>(<em>s</em>)<a class="reference internal" href="../_modules/django/utils/http.html#urlsafe_base64_decode"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.http.urlsafe_base64_decode" title="Permalink to this definition">¶</a></dt>
<dd><p>Decodes a base64 encoded string, adding back any trailing equal signs that
might have been stripped.</p>
<div class="versionchanged">
<span class="title">Changed in Django 2.2:</span> <p>In older versions, <code class="docutils literal notranslate"><span class="pre">s</span></code> may be a bytestring.</p>
</div>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.module_loading">
<span id="s-django-utils-module-loading"></span><span id="module-django.utils.module_loading"></span><span id="django-utils-module-loading"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.module_loading</span></code><a class="headerlink" href="#module-django.utils.module_loading" title="Permalink to this headline">¶</a></h2>
<p>Functions for working with Python modules.</p>
<dl class="function">
<dt id="django.utils.module_loading.import_string">
<code class="descname">import_string</code>(<em>dotted_path</em>)<a class="reference internal" href="../_modules/django/utils/module_loading.html#import_string"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.module_loading.import_string" title="Permalink to this definition">¶</a></dt>
<dd><p>Imports a dotted module path and returns the attribute/class designated by
the last name in the path. Raises <code class="docutils literal notranslate"><span class="pre">ImportError</span></code> if the import failed. For
example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.module_loading</span> <span class="k">import</span> <span class="n">import_string</span>
<span class="n">ValidationError</span> <span class="o">=</span> <span class="n">import_string</span><span class="p">(</span><span class="s1">&#39;django.core.exceptions.ValidationError&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>is equivalent to:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="n">ValidationError</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.safestring">
<span id="s-django-utils-safestring"></span><span id="module-django.utils.safestring"></span><span id="django-utils-safestring"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.safestring</span></code><a class="headerlink" href="#module-django.utils.safestring" title="Permalink to this headline">¶</a></h2>
<p>Functions and classes for working with “safe strings”: strings that can be
displayed safely without further escaping in HTML. Marking something as a “safe
string” means that the producer of the string has already turned characters
that should not be interpreted by the HTML engine (e.g. ‘&lt;’) into the
appropriate entities.</p>
<dl class="class">
<dt id="django.utils.safestring.SafeString">
<em class="property">class </em><code class="descname">SafeString</code><a class="headerlink" href="#django.utils.safestring.SafeString" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">str</span></code> subclass that has been specifically marked as “safe”
(requires no further escaping) for HTML output purposes. Alias of
<a class="reference internal" href="#django.utils.safestring.SafeText" title="django.utils.safestring.SafeText"><code class="xref py py-class docutils literal notranslate"><span class="pre">SafeText</span></code></a>.</p>
</dd></dl>

<dl class="class">
<dt id="django.utils.safestring.SafeText">
<em class="property">class </em><code class="descname">SafeText</code><a class="reference internal" href="../_modules/django/utils/safestring.html#SafeText"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.safestring.SafeText" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">str</span></code> subclass that has been specifically marked as “safe” for HTML
output purposes.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.safestring.mark_safe">
<code class="descname">mark_safe</code>(<em>s</em>)<a class="reference internal" href="../_modules/django/utils/safestring.html#mark_safe"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.safestring.mark_safe" title="Permalink to this definition">¶</a></dt>
<dd><p>Explicitly mark a string as safe for (HTML) output purposes. The returned
object can be used everywhere a string is appropriate.</p>
<p>Can be called multiple times on a single string.</p>
<p>Can also be used as a decorator.</p>
<p>For building up fragments of HTML, you should normally be using
<a class="reference internal" href="#django.utils.html.format_html" title="django.utils.html.format_html"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.utils.html.format_html()</span></code></a> instead.</p>
<p>String marked safe will become unsafe again if modified. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">mystr</span> <span class="o">=</span> <span class="s1">&#39;&lt;b&gt;Hello World&lt;/b&gt;   &#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mystr</span> <span class="o">=</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">mystr</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">mystr</span><span class="p">)</span>
<span class="go">&lt;class &#39;django.utils.safestring.SafeText&#39;&gt;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">mystr</span> <span class="o">=</span> <span class="n">mystr</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>  <span class="c1"># removing whitespace</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">mystr</span><span class="p">)</span>
<span class="go">&lt;type &#39;str&#39;&gt;</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.text">
<span id="s-django-utils-text"></span><span id="module-django.utils.text"></span><span id="django-utils-text"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.text</span></code><a class="headerlink" href="#module-django.utils.text" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="django.utils.text.format_lazy">
<code class="descname">format_lazy</code>(<em>format_string</em>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.utils.text.format_lazy" title="Permalink to this definition">¶</a></dt>
<dd><p>A version of <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str.format" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> for when <code class="docutils literal notranslate"><span class="pre">format_string</span></code>, <code class="docutils literal notranslate"><span class="pre">args</span></code>,
and/or <code class="docutils literal notranslate"><span class="pre">kwargs</span></code> contain lazy objects. The first argument is the string to
be formatted. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.text</span> <span class="k">import</span> <span class="n">format_lazy</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">pgettext_lazy</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="n">format_lazy</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{person}</span><span class="s1">/&lt;int:pk&gt;/&#39;</span><span class="p">,</span> <span class="n">person</span><span class="o">=</span><span class="n">pgettext_lazy</span><span class="p">(</span><span class="s1">&#39;URL&#39;</span><span class="p">,</span> <span class="s1">&#39;person&#39;</span><span class="p">)),</span>
         <span class="n">PersonDetailView</span><span class="o">.</span><span class="n">as_view</span><span class="p">()),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>This example allows translators to translate part of the URL. If “person”
is translated to “persona”, the regular expression will match
<code class="docutils literal notranslate"><span class="pre">persona/(?P&lt;pk&gt;\d+)/$</span></code>, e.g. <code class="docutils literal notranslate"><span class="pre">persona/5/</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.text.slugify">
<code class="descname">slugify</code>(<em>value</em>, <em>allow_unicode=False</em>)<a class="reference internal" href="../_modules/django/utils/text.html#slugify"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.text.slugify" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts a string to a URL slug by:</p>
<ol class="arabic simple">
<li>Converting to ASCII if <code class="docutils literal notranslate"><span class="pre">allow_unicode</span></code> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (the default).</li>
<li>Removing characters that aren’t alphanumerics, underscores, hyphens, or
whitespace.</li>
<li>Removing leading and trailing whitespace.</li>
<li>Converting to lowercase.</li>
<li>Replacing any whitespace or repeated dashes with single dashes.</li>
</ol>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">slugify</span><span class="p">(</span><span class="s1">&#39; Joel is a slug &#39;</span><span class="p">)</span>
<span class="go">&#39;joel-is-a-slug&#39;</span>
</pre></div>
</div>
<p>If you want to allow Unicode characters, pass <code class="docutils literal notranslate"><span class="pre">allow_unicode=True</span></code>. For
example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">slugify</span><span class="p">(</span><span class="s1">&#39;你好 World&#39;</span><span class="p">,</span> <span class="n">allow_unicode</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">&#39;你好-world&#39;</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.timezone">
<span id="s-django-utils-timezone"></span><span id="s-time-zone-selection-functions"></span><span id="module-django.utils.timezone"></span><span id="django-utils-timezone"></span><span id="time-zone-selection-functions"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.timezone</span></code><a class="headerlink" href="#module-django.utils.timezone" title="Permalink to this headline">¶</a></h2>
<dl class="data">
<dt id="django.utils.timezone.utc">
<code class="descname">utc</code><a class="headerlink" href="#django.utils.timezone.utc" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.tzinfo" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> instance that represents UTC.</p>
</dd></dl>

<dl class="class">
<dt id="django.utils.timezone.FixedOffset">
<em class="property">class </em><code class="descname">FixedOffset</code>(<em>offset=None</em>, <em>name=None</em>)<a class="reference internal" href="../_modules/django/utils/timezone.html#FixedOffset"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.FixedOffset" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.tzinfo" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass modeling a fixed offset from UTC.
<code class="docutils literal notranslate"><span class="pre">offset</span></code> is an integer number of minutes east of UTC.</p>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 2.2: </span>Use <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.timezone" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.timezone</span></code></a> instead.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.get_fixed_timezone">
<code class="descname">get_fixed_timezone</code>(<em>offset</em>)<a class="reference internal" href="../_modules/django/utils/timezone.html#get_fixed_timezone"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.get_fixed_timezone" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.tzinfo" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> instance that represents a time zone
with a fixed offset from UTC.</p>
<p><code class="docutils literal notranslate"><span class="pre">offset</span></code> is a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.timedelta" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.timedelta</span></code></a> or an integer number of
minutes. Use positive values for time zones east of UTC and negative
values for west of UTC.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.get_default_timezone">
<code class="descname">get_default_timezone</code>()<a class="reference internal" href="../_modules/django/utils/timezone.html#get_default_timezone"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.get_default_timezone" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.tzinfo" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> instance that represents the
<a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">default time zone</span></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.get_default_timezone_name">
<code class="descname">get_default_timezone_name</code>()<a class="reference internal" href="../_modules/django/utils/timezone.html#get_default_timezone_name"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.get_default_timezone_name" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the name of the <a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">default time zone</span></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.get_current_timezone">
<code class="descname">get_current_timezone</code>()<a class="reference internal" href="../_modules/django/utils/timezone.html#get_current_timezone"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.get_current_timezone" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.tzinfo" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> instance that represents the
<a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.get_current_timezone_name">
<code class="descname">get_current_timezone_name</code>()<a class="reference internal" href="../_modules/django/utils/timezone.html#get_current_timezone_name"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.get_current_timezone_name" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the name of the <a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.activate">
<code class="descname">activate</code>(<em>timezone</em>)<a class="reference internal" href="../_modules/django/utils/timezone.html#activate"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.activate" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the <a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a>. The
<code class="docutils literal notranslate"><span class="pre">timezone</span></code> argument must be an instance of a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.tzinfo" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a>
subclass or a time zone name.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.deactivate">
<code class="descname">deactivate</code>()<a class="reference internal" href="../_modules/django/utils/timezone.html#deactivate"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.deactivate" title="Permalink to this definition">¶</a></dt>
<dd><p>Unsets the <a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.override">
<code class="descname">override</code>(<em>timezone</em>)<a class="reference internal" href="../_modules/django/utils/timezone.html#override"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.override" title="Permalink to this definition">¶</a></dt>
<dd><p>This is a Python context manager that sets the <a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a> on entry with <a class="reference internal" href="#django.utils.timezone.activate" title="django.utils.timezone.activate"><code class="xref py py-func docutils literal notranslate"><span class="pre">activate()</span></code></a>, and restores
the previously active time zone on exit. If the <code class="docutils literal notranslate"><span class="pre">timezone</span></code> argument is
<code class="docutils literal notranslate"><span class="pre">None</span></code>, the <a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a> is unset
on entry with <a class="reference internal" href="#django.utils.timezone.deactivate" title="django.utils.timezone.deactivate"><code class="xref py py-func docutils literal notranslate"><span class="pre">deactivate()</span></code></a> instead.</p>
<p><code class="docutils literal notranslate"><span class="pre">override</span></code> is also usable as a function decorator.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.localtime">
<code class="descname">localtime</code>(<em>value=None</em>, <em>timezone=None</em>)<a class="reference internal" href="../_modules/django/utils/timezone.html#localtime"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.localtime" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts an aware <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> to a different time zone,
by default the <a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a>.</p>
<p>When <code class="docutils literal notranslate"><span class="pre">value</span></code> is omitted, it defaults to <a class="reference internal" href="#django.utils.timezone.now" title="django.utils.timezone.now"><code class="xref py py-func docutils literal notranslate"><span class="pre">now()</span></code></a>.</p>
<p>This function doesn’t work on naive datetimes; use <a class="reference internal" href="#django.utils.timezone.make_aware" title="django.utils.timezone.make_aware"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_aware()</span></code></a>
instead.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.localdate">
<code class="descname">localdate</code>(<em>value=None</em>, <em>timezone=None</em>)<a class="reference internal" href="../_modules/django/utils/timezone.html#localdate"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.localdate" title="Permalink to this definition">¶</a></dt>
<dd><p>Uses <a class="reference internal" href="#django.utils.timezone.localtime" title="django.utils.timezone.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a> to convert an aware <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> to a
<a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime.date" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date()</span></code></a> in a different time zone, by default the
<a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a>.</p>
<p>When <code class="docutils literal notranslate"><span class="pre">value</span></code> is omitted, it defaults to <a class="reference internal" href="#django.utils.timezone.now" title="django.utils.timezone.now"><code class="xref py py-func docutils literal notranslate"><span class="pre">now()</span></code></a>.</p>
<p>This function doesn’t work on naive datetimes.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.now">
<code class="descname">now</code>()<a class="reference internal" href="../_modules/django/utils/timezone.html#now"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.now" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> that represents the
current point in time. Exactly what’s returned depends on the value of
<a class="reference internal" href="settings.html#std:setting-USE_TZ"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_TZ</span></code></a>:</p>
<ul class="simple">
<li>If <a class="reference internal" href="settings.html#std:setting-USE_TZ"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_TZ</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, this will be a
<a class="reference internal" href="../topics/i18n/timezones.html#naive-vs-aware-datetimes"><span class="std std-ref">naive</span></a> datetime (i.e. a datetime
without an associated timezone) that represents the current time
in the system’s local timezone.</li>
<li>If <a class="reference internal" href="settings.html#std:setting-USE_TZ"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_TZ</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, this will be an
<a class="reference internal" href="../topics/i18n/timezones.html#naive-vs-aware-datetimes"><span class="std std-ref">aware</span></a> datetime representing the
current time in UTC. Note that <a class="reference internal" href="#django.utils.timezone.now" title="django.utils.timezone.now"><code class="xref py py-func docutils literal notranslate"><span class="pre">now()</span></code></a> will always return
times in UTC regardless of the value of <a class="reference internal" href="settings.html#std:setting-TIME_ZONE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TIME_ZONE</span></code></a>;
you can use <a class="reference internal" href="#django.utils.timezone.localtime" title="django.utils.timezone.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a> to get the time in the current time zone.</li>
</ul>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.is_aware">
<code class="descname">is_aware</code>(<em>value</em>)<a class="reference internal" href="../_modules/django/utils/timezone.html#is_aware"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.is_aware" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if <code class="docutils literal notranslate"><span class="pre">value</span></code> is aware, <code class="docutils literal notranslate"><span class="pre">False</span></code> if it is naive. This
function assumes that <code class="docutils literal notranslate"><span class="pre">value</span></code> is a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.is_naive">
<code class="descname">is_naive</code>(<em>value</em>)<a class="reference internal" href="../_modules/django/utils/timezone.html#is_naive"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.is_naive" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if <code class="docutils literal notranslate"><span class="pre">value</span></code> is naive, <code class="docutils literal notranslate"><span class="pre">False</span></code> if it is aware. This
function assumes that <code class="docutils literal notranslate"><span class="pre">value</span></code> is a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.make_aware">
<code class="descname">make_aware</code>(<em>value</em>, <em>timezone=None</em>, <em>is_dst=None</em>)<a class="reference internal" href="../_modules/django/utils/timezone.html#make_aware"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.make_aware" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an aware <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> that represents the same
point in time as <code class="docutils literal notranslate"><span class="pre">value</span></code> in <code class="docutils literal notranslate"><span class="pre">timezone</span></code>, <code class="docutils literal notranslate"><span class="pre">value</span></code> being a naive
<a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>. If <code class="docutils literal notranslate"><span class="pre">timezone</span></code> is set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, it
defaults to the <a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a>.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">pytz.AmbiguousTimeError</span></code> exception is raised if you try to make
<code class="docutils literal notranslate"><span class="pre">value</span></code> aware during a DST transition where the same time occurs twice
(when reverting from DST). Setting <code class="docutils literal notranslate"><span class="pre">is_dst</span></code> to <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code> will
avoid the exception by choosing if the time is pre-transition or
post-transition respectively.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">pytz.NonExistentTimeError</span></code> exception is raised if you try to make
<code class="docutils literal notranslate"><span class="pre">value</span></code> aware during a DST transition such that the time never occurred.
For example, if the 2:00 hour is skipped during a DST transition, trying to
make 2:30 aware in that time zone will raise an exception. To avoid that
you can use <code class="docutils literal notranslate"><span class="pre">is_dst</span></code> to specify how <code class="docutils literal notranslate"><span class="pre">make_aware()</span></code> should interpret
such a nonexistent time. If <code class="docutils literal notranslate"><span class="pre">is_dst=True</span></code> then the above time would be
interpreted as 2:30 DST time (equivalent to 1:30 local time). Conversely,
if <code class="docutils literal notranslate"><span class="pre">is_dst=False</span></code> the time would be interpreted as 2:30 standard time
(equivalent to 3:30 local time).</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.timezone.make_naive">
<code class="descname">make_naive</code>(<em>value</em>, <em>timezone=None</em>)<a class="reference internal" href="../_modules/django/utils/timezone.html#make_naive"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.timezone.make_naive" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a naive <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> that represents in
<code class="docutils literal notranslate"><span class="pre">timezone</span></code>  the same point in time as <code class="docutils literal notranslate"><span class="pre">value</span></code>, <code class="docutils literal notranslate"><span class="pre">value</span></code> being an
aware <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>. If <code class="docutils literal notranslate"><span class="pre">timezone</span></code> is set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, it
defaults to the <a class="reference internal" href="../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a>.</p>
</dd></dl>

</div>
<div class="section" id="s-module-django.utils.translation">
<span id="s-django-utils-translation"></span><span id="module-django.utils.translation"></span><span id="django-utils-translation"></span><h2><code class="docutils literal notranslate"><span class="pre">django.utils.translation</span></code><a class="headerlink" href="#module-django.utils.translation" title="Permalink to this headline">¶</a></h2>
<p>For a complete discussion on the usage of the following see the
<a class="reference internal" href="../topics/i18n/translation.html"><span class="doc">translation documentation</span></a>.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">u</span></code> prefix on the functions below comes from a difference in Python 2
between unicode and bytestrings. If your code doesn’t support Python 2, use the
functions without the <code class="docutils literal notranslate"><span class="pre">u</span></code>.</p>
<dl class="function">
<dt id="django.utils.translation.gettext">
<code class="descname">gettext</code>(<em>message</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#gettext"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.gettext" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="django.utils.translation.ugettext">
<code class="descname">ugettext</code>(<em>message</em>)<a class="headerlink" href="#django.utils.translation.ugettext" title="Permalink to this definition">¶</a></dt>
<dd><p>Translates <code class="docutils literal notranslate"><span class="pre">message</span></code> and returns it as a string.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.pgettext">
<code class="descname">pgettext</code>(<em>context</em>, <em>message</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#pgettext"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.pgettext" title="Permalink to this definition">¶</a></dt>
<dd><p>Translates <code class="docutils literal notranslate"><span class="pre">message</span></code> given the <code class="docutils literal notranslate"><span class="pre">context</span></code> and returns it as a string.</p>
<p>For more information, see <a class="reference internal" href="../topics/i18n/translation.html#contextual-markers"><span class="std std-ref">Contextual markers</span></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.gettext_lazy">
<code class="descname">gettext_lazy</code>(<em>message</em>)<a class="headerlink" href="#django.utils.translation.gettext_lazy" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="django.utils.translation.ugettext_lazy">
<code class="descname">ugettext_lazy</code>(<em>message</em>)<a class="headerlink" href="#django.utils.translation.ugettext_lazy" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="django.utils.translation.pgettext_lazy">
<code class="descname">pgettext_lazy</code>(<em>context</em>, <em>message</em>)<a class="headerlink" href="#django.utils.translation.pgettext_lazy" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as the non-lazy versions above, but using lazy execution.</p>
<p>See <a class="reference internal" href="../topics/i18n/translation.html#lazy-translations"><span class="std std-ref">lazy translations documentation</span></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.gettext_noop">
<code class="descname">gettext_noop</code>(<em>message</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#gettext_noop"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.gettext_noop" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="django.utils.translation.ugettext_noop">
<code class="descname">ugettext_noop</code>(<em>message</em>)<a class="headerlink" href="#django.utils.translation.ugettext_noop" title="Permalink to this definition">¶</a></dt>
<dd><p>Marks strings for translation but doesn’t translate them now. This can be
used to store strings in global variables that should stay in the base
language (because they might be used externally) and will be translated
later.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.ngettext">
<code class="descname">ngettext</code>(<em>singular</em>, <em>plural</em>, <em>number</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#ngettext"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.ngettext" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="django.utils.translation.ungettext">
<code class="descname">ungettext</code>(<em>singular</em>, <em>plural</em>, <em>number</em>)<a class="headerlink" href="#django.utils.translation.ungettext" title="Permalink to this definition">¶</a></dt>
<dd><p>Translates <code class="docutils literal notranslate"><span class="pre">singular</span></code> and <code class="docutils literal notranslate"><span class="pre">plural</span></code> and returns the appropriate string
based on <code class="docutils literal notranslate"><span class="pre">number</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.npgettext">
<code class="descname">npgettext</code>(<em>context</em>, <em>singular</em>, <em>plural</em>, <em>number</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#npgettext"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.npgettext" title="Permalink to this definition">¶</a></dt>
<dd><p>Translates <code class="docutils literal notranslate"><span class="pre">singular</span></code> and <code class="docutils literal notranslate"><span class="pre">plural</span></code> and returns the appropriate string
based on <code class="docutils literal notranslate"><span class="pre">number</span></code> and the <code class="docutils literal notranslate"><span class="pre">context</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.ngettext_lazy">
<code class="descname">ngettext_lazy</code>(<em>singular</em>, <em>plural</em>, <em>number</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#ngettext_lazy"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.ngettext_lazy" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="django.utils.translation.ungettext_lazy">
<code class="descname">ungettext_lazy</code>(<em>singular</em>, <em>plural</em>, <em>number</em>)<a class="headerlink" href="#django.utils.translation.ungettext_lazy" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="django.utils.translation.npgettext_lazy">
<code class="descname">npgettext_lazy</code>(<em>context</em>, <em>singular</em>, <em>plural</em>, <em>number</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#npgettext_lazy"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.npgettext_lazy" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as the non-lazy versions above, but using lazy execution.</p>
<p>See <a class="reference internal" href="../topics/i18n/translation.html#lazy-translations"><span class="std std-ref">lazy translations documentation</span></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.activate">
<code class="descname">activate</code>(<em>language</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#activate"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.activate" title="Permalink to this definition">¶</a></dt>
<dd><p>Fetches the translation object for a given language and activates it as
the current translation object for the current thread.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.deactivate">
<code class="descname">deactivate</code>()<a class="reference internal" href="../_modules/django/utils/translation.html#deactivate"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.deactivate" title="Permalink to this definition">¶</a></dt>
<dd><p>Deactivates the currently active translation object so that further _ calls
will resolve against the default translation object, again.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.deactivate_all">
<code class="descname">deactivate_all</code>()<a class="reference internal" href="../_modules/django/utils/translation.html#deactivate_all"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.deactivate_all" title="Permalink to this definition">¶</a></dt>
<dd><p>Makes the active translation object a <code class="docutils literal notranslate"><span class="pre">NullTranslations()</span></code> instance.
This is useful when we want delayed translations to appear as the original
string for some reason.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.override">
<code class="descname">override</code>(<em>language</em>, <em>deactivate=False</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#override"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.override" title="Permalink to this definition">¶</a></dt>
<dd><p>A Python context manager that uses
<a class="reference internal" href="#django.utils.translation.activate" title="django.utils.translation.activate"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.utils.translation.activate()</span></code></a> to fetch the translation object
for a given language, activates it as the translation object for the
current thread and reactivates the previous active language on exit.
Optionally, it can simply deactivate the temporary translation on exit with
<a class="reference internal" href="#django.utils.translation.deactivate" title="django.utils.translation.deactivate"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.utils.translation.deactivate()</span></code></a> if the <code class="docutils literal notranslate"><span class="pre">deactivate</span></code> argument
is <code class="docutils literal notranslate"><span class="pre">True</span></code>. If you pass <code class="docutils literal notranslate"><span class="pre">None</span></code> as the language argument, a
<code class="docutils literal notranslate"><span class="pre">NullTranslations()</span></code> instance is activated within the context.</p>
<p><code class="docutils literal notranslate"><span class="pre">override</span></code> is also usable as a function decorator.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.check_for_language">
<code class="descname">check_for_language</code>(<em>lang_code</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#check_for_language"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.check_for_language" title="Permalink to this definition">¶</a></dt>
<dd><p>Checks whether there is a global language file for the given language
code (e.g. ‘fr’, ‘pt_BR’). This is used to decide whether a user-provided
language is available.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.get_language">
<code class="descname">get_language</code>()<a class="reference internal" href="../_modules/django/utils/translation.html#get_language"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.get_language" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the currently selected language code. Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> if
translations are temporarily deactivated (by <a class="reference internal" href="#django.utils.translation.deactivate_all" title="django.utils.translation.deactivate_all"><code class="xref py py-func docutils literal notranslate"><span class="pre">deactivate_all()</span></code></a> or
when <code class="docutils literal notranslate"><span class="pre">None</span></code> is passed to <a class="reference internal" href="#django.utils.translation.override" title="django.utils.translation.override"><code class="xref py py-func docutils literal notranslate"><span class="pre">override()</span></code></a>).</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.get_language_bidi">
<code class="descname">get_language_bidi</code>()<a class="reference internal" href="../_modules/django/utils/translation.html#get_language_bidi"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.get_language_bidi" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns selected language’s BiDi layout:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">False</span></code> = left-to-right layout</li>
<li><code class="docutils literal notranslate"><span class="pre">True</span></code> = right-to-left layout</li>
</ul>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.get_language_from_request">
<code class="descname">get_language_from_request</code>(<em>request</em>, <em>check_path=False</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#get_language_from_request"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.get_language_from_request" title="Permalink to this definition">¶</a></dt>
<dd><p>Analyzes the request to find what language the user wants the system to
show. Only languages listed in settings.LANGUAGES are taken into account.
If the user requests a sublanguage where we have a main language, we send
out the main language.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">check_path</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the function first checks the requested URL
for whether its path begins with a language code listed in the
<a class="reference internal" href="settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGES</span></code></a> setting.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.get_supported_language_variant">
<code class="descname">get_supported_language_variant</code>(<em>lang_code</em>, <em>strict=False</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#get_supported_language_variant"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.get_supported_language_variant" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 2.1.</span> </div>
<p>Returns <code class="docutils literal notranslate"><span class="pre">lang_code</span></code> if it’s in the <a class="reference internal" href="settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGES</span></code></a> setting, possibly
selecting a more generic variant. For example, <code class="docutils literal notranslate"><span class="pre">'es'</span></code> is returned if
<code class="docutils literal notranslate"><span class="pre">lang_code</span></code> is <code class="docutils literal notranslate"><span class="pre">'es-ar'</span></code> and <code class="docutils literal notranslate"><span class="pre">'es'</span></code> is in <a class="reference internal" href="settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGES</span></code></a> but
<code class="docutils literal notranslate"><span class="pre">'es-ar'</span></code> isn’t.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">strict</span></code> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (the default), a country-specific variant may
be returned when neither the language code nor its generic variant is found.
For example, if only <code class="docutils literal notranslate"><span class="pre">'es-co'</span></code> is in <a class="reference internal" href="settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGES</span></code></a>, that’s
returned for <code class="docutils literal notranslate"><span class="pre">lang_code</span></code>s like <code class="docutils literal notranslate"><span class="pre">'es'</span></code> and <code class="docutils literal notranslate"><span class="pre">'es-ar'</span></code>. Those matches
aren’t returned if <code class="docutils literal notranslate"><span class="pre">strict=True</span></code>.</p>
<p>Raises <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#LookupError" title="(in Python v3.8)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">LookupError</span></code></a> if nothing is found.</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.to_locale">
<code class="descname">to_locale</code>(<em>language</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#to_locale"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.to_locale" title="Permalink to this definition">¶</a></dt>
<dd><p>Turns a language name (en-us) into a locale name (en_US).</p>
</dd></dl>

<dl class="function">
<dt id="django.utils.translation.templatize">
<code class="descname">templatize</code>(<em>src</em>)<a class="reference internal" href="../_modules/django/utils/translation.html#templatize"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.templatize" title="Permalink to this definition">¶</a></dt>
<dd><p>Turns a Django template into something that is understood by <code class="docutils literal notranslate"><span class="pre">xgettext</span></code>.
It does so by translating the Django translation tags into standard
<code class="docutils literal notranslate"><span class="pre">gettext</span></code> function invocations.</p>
</dd></dl>

<dl class="data">
<dt id="django.utils.translation.LANGUAGE_SESSION_KEY">
<code class="descname">LANGUAGE_SESSION_KEY</code><a class="headerlink" href="#django.utils.translation.LANGUAGE_SESSION_KEY" title="Permalink to this definition">¶</a></dt>
<dd><p>Session key under which the active language for the current session is
stored.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Django Utils</a><ul>
<li><a class="reference internal" href="#module-django.utils.cache"><code class="docutils literal notranslate"><span class="pre">django.utils.cache</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.dateparse"><code class="docutils literal notranslate"><span class="pre">django.utils.dateparse</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.decorators"><code class="docutils literal notranslate"><span class="pre">django.utils.decorators</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.encoding"><code class="docutils literal notranslate"><span class="pre">django.utils.encoding</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.feedgenerator"><code class="docutils literal notranslate"><span class="pre">django.utils.feedgenerator</span></code></a><ul>
<li><a class="reference internal" href="#syndicationfeed"><code class="docutils literal notranslate"><span class="pre">SyndicationFeed</span></code></a></li>
<li><a class="reference internal" href="#enclosure"><code class="docutils literal notranslate"><span class="pre">Enclosure</span></code></a></li>
<li><a class="reference internal" href="#rssfeed"><code class="docutils literal notranslate"><span class="pre">RssFeed</span></code></a></li>
<li><a class="reference internal" href="#rss201rev2feed"><code class="docutils literal notranslate"><span class="pre">Rss201rev2Feed</span></code></a></li>
<li><a class="reference internal" href="#rssuserland091feed"><code class="docutils literal notranslate"><span class="pre">RssUserland091Feed</span></code></a></li>
<li><a class="reference internal" href="#atom1feed"><code class="docutils literal notranslate"><span class="pre">Atom1Feed</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-django.utils.functional"><code class="docutils literal notranslate"><span class="pre">django.utils.functional</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.html"><code class="docutils literal notranslate"><span class="pre">django.utils.html</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.http"><code class="docutils literal notranslate"><span class="pre">django.utils.http</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.module_loading"><code class="docutils literal notranslate"><span class="pre">django.utils.module_loading</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.safestring"><code class="docutils literal notranslate"><span class="pre">django.utils.safestring</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.text"><code class="docutils literal notranslate"><span class="pre">django.utils.text</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.timezone"><code class="docutils literal notranslate"><span class="pre">django.utils.timezone</span></code></a></li>
<li><a class="reference internal" href="#module-django.utils.translation"><code class="docutils literal notranslate"><span class="pre">django.utils.translation</span></code></a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="urls.html"
                        title="previous chapter"><code class="docutils literal notranslate"><span class="pre">django.urls</span></code> functions for use in URLconfs</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="validators.html"
                        title="next chapter">Validators</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/ref/utils.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="urls.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;django.urls&lt;/span&gt;&lt;/code&gt; functions for use in URLconfs">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="validators.html" title="Validators">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>